// Mouse Driver
// (c) jimon game studio

#include "CMouseDriver.h"
#include "CWindow_Win.h"

#include "ITimer.h"

#ifdef JE_DEVICE_LINUX
#include <GL/glx.h>
#endif

namespace je
{
	namespace input
	{

		//! Constructor
		CMouseDriver::CMouseDriver(device::IWindow * window)
			:CurrentWindow(window),MouseWheelPos(0),CursorVisible(true)
		{
		}

		//! Destructor
		CMouseDriver::~CMouseDriver()
		{
		}

		//! Set Position
		void CMouseDriver::SetPosition(const jeVector2df & NewPosition)
		{
			#ifdef JE_DEVICE_WIN
			f64 Time1 = core::GetTimer()->GetTime();
				if(CurrentWindow)
				{
					RECT rect;
					HWND win_hWnd = (static_cast<device::CWindow_Win*>(CurrentWindow))->GetWin32WindowHandle();
					if(win_hWnd && !CurrentWindow->IsFullScreen())
					{
						POINT point;
						point.x = (s32)NewPosition.X;
						point.y = (s32)NewPosition.Y;
						ClientToScreen(win_hWnd,LPPOINT(&point));
						s32 BorderX = GetSystemMetrics(SM_CXDLGFRAME);
						s32 BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYDLGFRAME);
						GetWindowRect(win_hWnd,&rect);
						SetCursorPos((s32)NewPosition.X + 1 + rect.left + BorderX, (s32)NewPosition.Y + rect.top + BorderY + 1);
					}
					else
						SetCursorPos((s32)NewPosition.X,(s32)NewPosition.Y);
				}
				else
					SetCursorPos((s32)NewPosition.X,(s32)NewPosition.Y);
			Time1 = core::GetTimer()->GetTime()-Time1;
			#endif


			#ifdef JE_DEVICE_LINUX
				XWarpPointer(static_cast<Display*>(CurrentWindow->GetWindowApiPointer(device::WAPT_X_DEVICE)),None,*static_cast<Window*>(CurrentWindow->GetWindowApiPointer(device::WAPT_X_WINDOW)),0,0,CurrentWindow->GetSize().X,CurrentWindow->GetSize().Y,NewPosition.X,NewPosition.Y);
				XFlush(static_cast<Display*>(CurrentWindow->GetWindowApiPointer(device::WAPT_X_DEVICE)));
			#endif
		}

		//! Get Position
		jeVector2df CMouseDriver::GetPosition()
		{
			#ifdef JE_DEVICE_WIN
				POINT point;
				RECT rect = RECT();
				s32 BorderX = 0;
				s32 BorderY = 0;
				GetCursorPos(&point);
				if(CurrentWindow)
				{
					if(!CurrentWindow->IsFullScreen())
					{
						BorderX = GetSystemMetrics(SM_CXDLGFRAME)+1;
						BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYDLGFRAME)+1;
					}
					HWND win_hWnd = (static_cast<device::CWindow_Win*>(CurrentWindow))->GetWin32WindowHandle();
					GetWindowRect(win_hWnd,&rect);
				}
				f32 posx = (f32)point.x - rect.left - BorderX;
				f32 posy = (f32)point.y - rect.top - BorderY;
				return jeVector2df(posx,posy);
			#endif

			#ifdef JE_DEVICE_LINUX
				Window TempWindow;
				s32 X,Y,RootX,RootY;
				u32 Mask;
				XQueryPointer(static_cast<Display*>(CurrentWindow->GetWindowApiPointer(device::WAPT_X_DEVICE)),*static_cast<Window*>(CurrentWindow->GetWindowApiPointer(device::WAPT_X_WINDOW)),&TempWindow,&TempWindow,&RootX,&RootY,&X,&Y,&Mask);
				return jeVector2df(X,Y);
			#endif
		}

		//! Get Mouse Wheel Position
		s32 CMouseDriver::GetMouseWheelPosition()
		{
			return MouseWheelPos;
		}

		//! Set cursor visible
		void CMouseDriver::SetCursorVisible(u1 Visible)
		{
			#ifdef JE_DEVICE_WIN
			ShowCursor(Visible);
			#endif

			#ifdef JE_DEVICE_LINUX
			// TODO
			#endif

			CursorVisible = Visible;
		}

		//! Return true if cursor visible
		u1 CMouseDriver::IsCursorVisible()
		{
			return CursorVisible;
		}

		//! Set Mouse Wheel Position
		void CMouseDriver::SetMouseWheelPosition(s32 NewPos)
		{
			MouseWheelPos = NewPos;
		}

	}
}

